import Vue from 'vue';
import ElementUI from 'element-ui';
import {debounce} from "./util";

export const loadingConfig = {
  lock: true,
  text: 'Loading',
  spinner: 'el-icon-loading',
  background: 'rgba(0, 0, 0, 0.7)'
}

var loading = null ,needLoadingRequsetCount = 0;
const loadingShow = () => {
  if(needLoadingRequsetCount==0&&!loading){
    loading = ElementUI.Loading.service(loadingConfig);
  }
  needLoadingRequsetCount++
}

const loadingHide = () => {
  Vue.nextTick(()=>{
    needLoadingRequsetCount--;
    needLoadingRequsetCount = Math.max(needLoadingRequsetCount,0);
    if(needLoadingRequsetCount==0&&loading){
      hideLoading()
    }
  })
}
var fn=()=>{
  loading.close();
  loading = null;
}
var hideLoading = debounce(fn,300);

const loadingObj={
  loadingShow,
  loadingHide
}

export default loadingObj
